﻿package effects
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import evo.engine.AbstractDemoEffect;
		
	public class FlashEffect extends AbstractDemoEffect
	{
		public var speed:Number=1;
		public var color:uint = 0xffffffff;
		public var done:Boolean=true;
		
		private var graph:Graphics;
		private var colorTransform:ColorTransform;
		private var flashMap:BitmapData;
		
		public function resetColor(red:Number, green:Number, blue:Number):void
		{
			color = (int(red*255) << 16) | (int(green*255) << 8) | (int(blue*255));
		}
			
		public override function init(params:Object=null):void
		{
			super.init(params);
			colorTransform = new ColorTransform(1,1,1,0);
		}
		
		override public function set output(buffer:BitmapData):void
		{
			super.output = buffer;
			flashMap = new BitmapData(buffer.width, buffer.height, true, 0);
		}
		
		public override function play(dt:Number):void
		{			
			if(done)
				return;
				
			colorTransform.alphaMultiplier -= dt/(1000*speed);
			if(colorTransform.alphaMultiplier < 0) {
				colorTransform.alphaMultiplier=0;
				done=true;
			}

			flashMap.fillRect(flashMap.rect, color);
			flashMap.colorTransform(flashMap.rect, colorTransform);
			
			/* Only attempt to draw when buffer is set */
			if(output != null)
			{
				output.draw(flashMap);
			}
		}
		
		public function reset():void
		{
			colorTransform.alphaMultiplier=1;
			done=false;
		}
	}
}